package net.cl.db.core;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.cl.Validator;
import net.cl.db.CachedResultSet;
import net.cl.db.Constants;
import net.cl.db.DBException;
import net.cl.db.ds.BasicDataSource;
import net.cl.db.sql.dialect.Dialect;
import net.cl.db.sql.dialect.MySqlDialect;
import net.cl.db.sql.dialect.UnknownDialect;
import net.cl.utils.StringUtil;
import net.cl.utils.SystemUtil;

public class DBTools {

	private static final Log log = LogFactory.getLog(DBTools.class);

	public static String resultLikeCSV(CachedResultSet rs) {
		if (rs == null)
			throw new NullPointerException();
		try {
			int columnCount = rs.getColumnCount();
			StringBuilder sb = new StringBuilder();
			for (int i = 1; i <= columnCount; i++) {
				sb.append(rs.getColumnName(i));
				if (i < columnCount)
					sb.append(",");
			}
			sb.append(SystemUtil.getLineSeparator());
			while (rs.next()) {
				for (int i = 1; i <= columnCount; i++) {
					if (Validator.isNotEmpty(rs.getString(i))) {
						sb.append("'");
						sb.append(rs.getString(i));
						sb.append("'");
					} else
						sb.append("null");
					if (i < columnCount)
						sb.append(",");
				}
				if (rs.isLast())
					return sb.toString();
				sb.append(SystemUtil.getLineSeparator());
			}
			return sb.toString();
		} catch (DBException e) {
			log.error(e);
			return null;
		}
	}

	public static String getDumpSql(CachedResultSet rs) {
		if (rs == null)
			throw new NullPointerException();
		try {
			int columnCount = rs.getColumnCount();
			StringBuilder sb = new StringBuilder();
			String tableName = rs.getTableName(columnCount);
			while (rs.next()) {
				sb.append("INSERT INTO ");
				sb.append(tableName);
				sb.append(" (");
				for (int i = 1; i <= columnCount; i++) {
					sb.append(rs.getColumnName(i));
					if (i < columnCount)
						sb.append(",");
				}
				sb.append(") VALUES (");
				for (int i = 1; i <= columnCount; i++) {
					if (Validator.isNotEmpty(rs.getString(i))) {
						sb.append("'");
						sb.append(rs.getString(i));
						sb.append("'");
					} else
						sb.append("null");
					if (i < columnCount)
						sb.append(",");
				}
				sb.append(");");
				if (rs.isLast())
					return sb.toString();
				sb.append(SystemUtil.getLineSeparator());
			}
			return sb.toString();
		} catch (DBException e) {
			log.error(e);
			return null;
		}
	}

	public static Dialect guessDialect(DataSource ds) {
		if (ds instanceof BasicDataSource) {
			BasicDataSource bds = (BasicDataSource) ds;
			String driver = bds.getDriverClassName();
			if (driver.toLowerCase().indexOf(Constants.DESC_MYSQL) > -1)
				return new MySqlDialect();
			else if (driver.toLowerCase().indexOf(Constants.DESC_ORACLE) > -1)
				return new MySqlDialect();
			else if (driver.toLowerCase().indexOf(Constants.DESC_SQLSERVER) > -1)
				return new MySqlDialect();
			else if (driver.toLowerCase().indexOf(Constants.DESC_ACCESS) > -1)
				return new MySqlDialect();
			else if (driver.toLowerCase().indexOf(Constants.DESC_SQLITE) > -1)
				return new MySqlDialect();
			else
				return new MySqlDialect();
		}
		return new UnknownDialect();
	}

}
